#ifndef ADMIN_VIEW_H
#define ADMIN_VIEW_H
#include <QMainWindow>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <QTableWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QListWidget>
#include <QMap>  // 引入QMap
#include "sqloperator.h" // 引入工具类
QT_BEGIN_NAMESPACE
namespace Ui { class admin_view; }
QT_END_NAMESPACE

// 通用数据管理基类（基于SqlOperator实现）
class GeneralDataManagementWidget : public QWidget {
    Q_OBJECT
public:
    GeneralDataManagementWidget(const QString& tableName, const QStringList& columnHeaders, QWidget *parent = 0);
    bool connectDatabase();
    void queryData1(const QMap<QString, QVariant> &whereConditions);
private slots:
    void onQueryClicked();
    void onAddClicked();
    void onDeleteClicked();
    void onSaveClicked();
    void onSearchClicked();
    void onResetSearchClicked();
protected:
    virtual QString getSearchSql(const QString& keyword);
    QTableWidget *dataTable;
    QPushButton *queryBtn;
    QPushButton *addBtn;
    QPushButton *deleteBtn;
    QPushButton *saveBtn;
    SqlOperator sqlOp; // 工具类实例
    QLineEdit *searchEdit;
    QPushButton *searchBtn;
    QPushButton *resetSearchBtn;
    QString tableName;
    QStringList columnHeaders;
    QListWidget *searchResultList;
    QMap<QString, QString> fieldMap; // 改用QMap确保顺序（表格列→数据库字段）
};

class UserManagementWidget : public GeneralDataManagementWidget {
    Q_OBJECT
public:
    UserManagementWidget(QWidget *parent = 0)
        : GeneralDataManagementWidget("user", QStringList() << "ID" << "用户名" << "账号" << "密码", parent) {}
protected:
    QString getSearchSql(const QString& keyword) override {
        // 显式指定字段顺序，与fieldMap保持一致
        QString fields = fieldMap.values().join(", ");
        return QString("SELECT %1 FROM user WHERE user_name LIKE '%%2%' OR account LIKE '%%2%'")
                .arg(fields, keyword);
    }
};

class ResetPasswordWidget : public QWidget {
    Q_OBJECT
public:
    ResetPasswordWidget(QWidget *parent = 0);
    bool connectDatabase();
private slots:
    void onResetClicked();
    void onTextChanged(const QString &text);
private:
    QLineEdit *accountEdit;
    QPushButton *resetBtn;
    SqlOperator sqlOp; // 工具类实例
    QListWidget *searchResultList;
};

class EquipmentManagementWidget : public GeneralDataManagementWidget {
    Q_OBJECT
public:
    EquipmentManagementWidget(QWidget *parent = 0)
        : GeneralDataManagementWidget("gear", QStringList() << "装备ID" << "装备名称" << "装备价值" << "装备槽ID", parent) {}
protected:
    QString getSearchSql(const QString& keyword) override {
        // 显式指定字段顺序
        QString fields = fieldMap.values().join(", ");
        return QString("SELECT %1 FROM gear WHERE gear_name LIKE '%%2%'").arg(fields, keyword);
    }
};

class MonsterManagementWidget : public GeneralDataManagementWidget {
    Q_OBJECT
public:
    MonsterManagementWidget(QWidget *parent = 0)
        : GeneralDataManagementWidget("monster", QStringList() << "怪物ID" << "怪物名称" << "怪物价值", parent) {}
protected:
    QString getSearchSql(const QString& keyword) override {
        // 显式指定字段顺序
        QString fields = fieldMap.values().join(", ");
        return QString("SELECT %1 FROM monster WHERE monster_name LIKE '%%2%'").arg(fields, keyword);
    }
};

class AdminView : public QMainWindow {
    Q_OBJECT
public:
    AdminView(QWidget *parent = 0);
    ~AdminView();
    static void showAdminWindow(QWidget *parent = nullptr);
signals:
    void signalClose();
private slots:
    void onUserManagementClicked();
    void onEquipmentManagementClicked();
    void onMonsterManagementClicked();
    void onResetPasswordClicked();
    void onExitClicked();
private:
    Ui::admin_view *ui;
    QMdiArea *mdiArea;
    QWidget *centralWidget;
    QHBoxLayout *mainLayout;
    QMdiSubWindow *userWindow;
    QMdiSubWindow *equipmentWindow;
    QMdiSubWindow *monsterWindow;
    QMdiSubWindow *resetPwdWindow;
    UserManagementWidget *userWidget;
    EquipmentManagementWidget *equipmentWidget;
    MonsterManagementWidget *monsterWidget;
    ResetPasswordWidget *resetPwdWidget;
};
#endif // ADMIN_VIEW_H

